package com.kky.pp.adapter

import android.annotation.SuppressLint
import android.app.Activity
import android.widget.TextView
import com.custom.base.base.BaseRecyclerAdapter
import com.custom.base.view.SuperViewHolder
import com.kky.pp.R
import com.kky.pp.base.GenderPreferListBean
import java.util.stream.Collectors

class PurposeAdapter(
    val activity: Activity,
    val listModel: ArrayList<GenderPreferListBean>,
    val choice: Boolean?,
    val isRequired: Boolean? = null,
    val onClickListener: OnClickListener? = null
) :
    BaseRecyclerAdapter<GenderPreferListBean>(listModel, activity) {
    var youAcceptForMan: ArrayList<GenderPreferListBean> = arrayListOf()
    var youAcceptForWomen: ArrayList<GenderPreferListBean> = arrayListOf()
    var youAcceptForQueer: ArrayList<GenderPreferListBean> = arrayListOf()
    override val layoutId: Int
        get() = R.layout.item_custom_selector

    override fun onBindItemHolder(
        position: Int,
        holder: SuperViewHolder,
        bean: GenderPreferListBean,
        payloads: List<Any>?
    ) {

        val itemText = holder.getView<TextView>(R.id.txtItemText)
        itemText.text = bean.value
        itemText.setTextColor(activity.getColor(if (bean.selected == 1) R.color.white else R.color.color_CAC7CD))
        itemText.setBackgroundResource(if (bean.selected == 1) R.drawable.ic_button_bg_radius_25 else R.drawable.shape_283040_radius_25)
        itemText.setOnClickListener {
            upDateStatus(position)
            onClickListener?.selected(position)
        }
    }

    fun getSingleSelect(): GenderPreferListBean {
        val singleSelect =
            dataList.stream().filter { it.selected == 1 }.collect(Collectors.toList())
        return singleSelect[0]
    }
    fun getSelect(): List<GenderPreferListBean> {
        return dataList.stream().filter { it.selected == 1 }.collect(Collectors.toList())
    }

    @SuppressLint("NotifyDataSetChanged")
    fun upDateStatus(position: Int): Boolean {
        if (choice == true) {
            //多选
            var rSelected = 0
            listModel.forEach {
                if (it.selected == 1) {
                    rSelected++
                }
            }
            if (isRequired == true && rSelected < 2) {
                return false
            } else {
                val selected = listModel.get(position).selected
                listModel.get(position).selected = if (selected == 0) 1 else 0
                notifyDataSetChanged()
                return true
            }
        } else {
            //单选
            listModel.stream().forEach {
                it.selected = 0
            }
            listModel.get(position).selected = 1
            notifyDataSetChanged()
            return true
        }
    }

    fun setAcceptForMan(youAcceptForMan: ArrayList<GenderPreferListBean>) {
        this.youAcceptForMan = youAcceptForMan
    }

    fun setAcceptForWomen(youAcceptForWomen: ArrayList<GenderPreferListBean>) {
        this.youAcceptForWomen = youAcceptForWomen
    }
    fun setAcceptForQueer(youAcceptForQueer: ArrayList<GenderPreferListBean>) {
        this.youAcceptForQueer = youAcceptForQueer
    }

    abstract class OnClickListener : Listener {
        //扩展

    }

    interface Listener {
        fun selected(position: Int)
    }
}